package cn.newstrength.shihaipeng.step;

import cn.newstrength.wtdf.plugin.redis.RedisKey;
import cn.newstrength.wtdf.plugin.service.RedisService;
import cn.newstrength.wtdf.plugin.util.SpringUtils;
import cn.newstrength.wtdf.plugin.util.TranUtils;
import cn.newstrength.wtdf.web.databus.DataContext;
import cn.newstrength.wtdf.web.exception.PAIException;
import cn.newstrength.wtdf.web.processor.Operation;
import cn.newstrength.wtdf.web.processor.OperationStep;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

public class RemoveCacheStep implements OperationStep {
    private static final Logger logger = LoggerFactory.getLogger(RemoveCacheStep.class);

    public RemoveCacheStep() {
    }

    public int excute(Operation oper) throws PAIException {
        Environment environment = (Environment)SpringUtils.getBean("environment", Environment.class);
        String appId = environment.getProperty("spring.application.name");
        RedisService redisService = (RedisService)SpringUtils.getBean("redisService", RedisService.class);
        DataContext context = oper.getContext();
        String cacheKey = context.getElementValue("resource");
        if (cacheKey != null) {
            TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {
            };
            List<String> keys = (List)TranUtils.jsonToBeanByTypeReference(cacheKey, typeReference);
            Iterator var9 = keys.iterator();

            while(var9.hasNext()) {
                String opId = (String)var9.next();
                String key = RedisKey.OPERATION_CACHE_KEY.getKey().concat(":").concat(appId).concat("[").concat(opId).concat("]");

                try {
                    boolean result = redisService.remove(key);
                    logger.info("移除缓存 Key -> {} {}!", key, result);
                } catch (Exception var13) {
                    var13.printStackTrace();
                }
            }
        }

        return 0;
    }

    public int init(Map<String, String> param) {
        return 0;
    }
}

